package com.vx.vxbug.controllers;

import com.vx.vxbug.BugPriority;
import com.vx.vxbug.BugStatus;
import com.vx.vxbug.entities.Bug;
import com.vx.vxbug.services.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vx.vxbug.services.BugService;
import com.vx.vxbug.services.ProgramService;
import com.vx.vxbug.services.UserService;

import java.util.Calendar;
import java.util.GregorianCalendar;

@Controller
public class BugsController {
	@Autowired
	private ProgramService pS;
	@Autowired
	private UserService uS;
	@Autowired
	private BugService bS;
    @Autowired
    private CommentService cS;

	@RequestMapping(value = { "/program/{p}" }, method = RequestMethod.GET)
	public String getBugs(@PathVariable("p") String p, Model model) {
		try {
            int idP = Integer.valueOf(p);
            model.addAttribute("bugs", bS.getAllBugsForProgramId(idP));
            model.addAttribute("p", pS.getProgramById(idP));
			return "program";
		} catch (Exception ex) {
			return "redirect:";
		}
	}

    @RequestMapping(value = { "/program/{p}/addBug" }, method = RequestMethod.GET)
    public String addBug(@PathVariable("p") String p, Model model) {
        try {
            int idP = Integer.valueOf(p);
            model.addAttribute("p", pS.getProgramById(idP));
            model.addAttribute("BugPriority", BugPriority.values());

            return "addBug";
        } catch (Exception ex) {
            return "redirect:";
        }
    }

    @RequestMapping(value = { "/program/{p}/addBug" }, method = RequestMethod.POST)
    public String addBugPost(@ModelAttribute("bug")
                                 Bug b, @PathVariable("p") String p, Model model) {
        try{
            GregorianCalendar gc = new GregorianCalendar();
            b.setDate(gc.getTime());
            b.setStatus(BugStatus.Open);
        bS.addBug(b);
        return "redirect:./";
        } catch (Exception ex){
            return "redirect:addBug";
        }

    }

    @RequestMapping(value = { "/program/{p}/bug/{b}/changeStatus" }, method = RequestMethod.POST)
    public String changeStatus(@ModelAttribute("status")
                                   BugStatus bstatus,@ModelAttribute("programVersionFixed") String pVersFix , @PathVariable("p") String p, @PathVariable("b") String b, Model model) {
        try {
            int idP = Integer.valueOf(p);
            int idB = Integer.valueOf(b);

            Bug a = bS.getBugById(idB);
            a.setStatus(bstatus);
            a.setProgramVersionFixed(pVersFix);
            bS.update(a);
            model.addAttribute("statuses", BugStatus.values());
            model.addAttribute("comments", cS.getAllCommentsForBugId(idB));
            model.addAttribute("b", bS.getBugById(idB));
            model.addAttribute("p", pS.getProgramById(idP));
            return "redirect:";
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return "redirect:";
        }
    }
}
